//
//  UIWindow+SGExtension.swift
//  QingLiuSwift
//
//  Created by xyanl on 2024/11/12.
//  Copyright © 2024 考拉🐨. All rights reserved.
//

import Foundation
import UIKit

//extension UIWindow: SpadgerCompatible {}

public extension Spadger where Base: UIWindow {
    
#if !SWIFTMESSAGES_APP_EXTENSIONS
    static var keyWindow: UIWindow {
        UIApplication.shared.connectedScenes
            .sorted { $0.activationState.sortPriority < $1.activationState.sortPriority }
            .compactMap { $0 as? UIWindowScene }
            .compactMap { $0.windows.first { $0.isKeyWindow } }.first ?? UIWindow()
    }
#endif
    
//    static var xKeyWindow: UIWindow {
//        if #available(iOS 13.0, *) {
//            UIApplication.shared.connectedScenes
//                .map({ $0 as? UIWindowScene })
//                .compactMap({ $0 })
//                .first?.windows.first ?? UIWindow()
//        } else {
//            UIApplication.shared.windows.first ?? UIWindow()
//        }
//    }
    
}

@available(iOS 13.0, *)
private extension UIScene.ActivationState {
    var sortPriority: Int {
        switch self {
        case .foregroundActive: return 1
        case .foregroundInactive: return 2
        case .background: return 3
        case .unattached: return 4
        @unknown default: return 5
        }
    }
}
